
package codeGenerate.json.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import codeGenerate.common.utils.SystemUtils;
import codeGenerate.json.converter.JsonToClassConverter;
import codeGenerate.json.generater.JsonGenerater;
import codeGenerate.json.tempalte.TemPathJson;
import codeGenerate.json.vo.JavaClassVo;
import codeGenerate.json.vo.JavaFieldVo;
import codeGenerate.json.vo.JsonConfigVo;

/**
 * json工具类
 */
public class JsonGenUtils {

	/**
	 * 生成代码
	 * @author fudewei
	 * @param configPath 配置文件路径
	 */
	public static void genCode(String configPath) throws Exception {
		File file = new File(configPath);
		if (!file.exists()) {
			System.out.println("文件不存在：" + configPath);
		}
		FileInputStream is = new FileInputStream(file);
		genCode(is);
	}

	/**
	 * 生成代码
	 * @author fudewei
	 * @param is  输入流
	 */
	public static void genCode(InputStream is) throws Exception {
		List<JsonConfigVo> configList = readConfig(is);
		for (JsonConfigVo cf : configList) {
			JsonGenerater gen = new JsonGenerater();
			gen.setConfigVo(cf);
			gen.generateCode();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<JsonConfigVo> readConfig(InputStream is) throws ParserConfigurationException, SAXException, IOException, DocumentException {
		try {
			List<JsonConfigVo> configVoList = new ArrayList<>();
			//创建解析器
			SAXReader reader = new SAXReader();
			//得到document
			Document document = reader.read(is);
			Element root = document.getRootElement();
			String createUser = root.elementText("createUser");
			String copyright = root.elementText("copyright");
			String genPath = root.elementText("genPath");

			String template = StringUtils.defaultIfBlank(root.elementText("template"), TemPathJson.class.getName());
			String templateDirectory = StringUtils.defaultIfBlank(root.elementText("templateDirectory"), SystemUtils.defalutTemplateDirectory());

			List<Element> beanEles = root.element("beans").elements("bean");
			for (Element beanEle : beanEles) {
				JsonConfigVo cv = new JsonConfigVo();
				cv.setCreateUser(createUser);
				cv.setCopyright(copyright);
				cv.setTemplate(template);
				cv.setGenPath(genPath);

				cv.setTemplateDirectory(templateDirectory);
				cv.setCreateTime(new Date());
				cv.setBeanDescription(beanEle.attributeValue("desc"));
				cv.setBeanName(StringUtils.defaultIfBlank(beanEle.attributeValue("beanName"), SystemUtils.getBeanNameByPath(beanEle.attributeValue("path"))));
				cv.setBeanNamePrefix(StringUtils.defaultIfBlank(beanEle.attributeValue("beanNamePrefix"), ""));
				cv.setPack(beanEle.attributeValue("path"));
				List<JavaClassVo> javaClassList = new JsonToClassConverter(cv).convert(beanEle.getTextTrim());
				cv.setJavaClassList(javaClassList);
				configVoList.add(cv);
			}
			return configVoList;
		} finally {
			is.close();
		}

	}

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, DocumentException {
		File file = new File("D:\\jdbc.xml");
		FileInputStream is = new FileInputStream(file);
		List<JsonConfigVo> readConfig = readConfig(is);
		for (JsonConfigVo jsonConfigVo : readConfig) {
			List<JavaClassVo> javaClassList = jsonConfigVo.getJavaClassList();
			for (JavaClassVo javaClass : javaClassList) {
				System.out.println("===================" + javaClass.getName());
				List<JavaFieldVo> javaFieldList = javaClass.getJavaFieldList();
				for (JavaFieldVo javaField : javaFieldList) {
					System.out.println(javaField);
				}
			}
		}
	}
}
